﻿using INTEX.DataBase;
using System;
using System.Data;
using System.Linq;
namespace WPFExcelReport
{

    public partial class MainViewModel
    {
        public DataSet PlanAB45(DataSet SetEnv, DataSet db, DataRow dwABA,DataRow dwRule)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            DataRow[] rows = null;
            DataRow dwBHSATZ = null;
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            DataRow dwAPZUORD = null;
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataRow dwMU = null;
            DataRow dwBSGROE = null;

            #region========Validate=================
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            if (LFD0.Trim() == "")
                LFD0 = ABPLFD;
            string POS0 = dwABA["ABAAUFPOS"].ToString();
            if (POS0.Trim() == "")
            {
                POS0 = ABPOS.ToString(); ABPOS++;
            }

            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();
         
            filter = "BHKART='" + ART + "' AND BHKVAR='" + VAR + "'";
            rows = db.Tables["BHSATZ"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwBHSATZ = row;
                break;
            }
            if (dwBHSATZ == null)
            {
                throw new System.Exception("没有对应的工艺档案");
                return SReturn;
            }
            filter = "APZSTUFE='K' AND APZART='" + ART + "' AND APZVAR='" + VAR + "' ";
            filter = "APZSTUFE='K' AND APZART='" + ART + "' ";
            rows = db.Tables["APZUORD"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwAPZUORD = row;
                break;
            }
            if (dwAPZUORD == null)
            {
                //throw new System.Exception("没有对应的工艺流程");
                //return SReturn;
                APZANRKRS = "SZ";
                APZALFDNR = "0001";
            }
            else
            {
                APZANRKRS = dwAPZUORD["APZANRKRS"].ToString();
                APZALFDNR = dwAPZUORD["APZALFDNR"].ToString();
            }
            filter = "BSGART='" + ART + "'";
            rows = db.Tables["BSGROE"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwBSGROE = row;
                break;
            } 
            filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
            rows = db.Tables["APFOLGE"].Select(filter);
            foreach (DataRow row in rows)
            {
                PRLO = row["KSZLO"].ToString();
                PRLB = row["KSZLB"].ToString();
                PRLP = row["KSZLP"].ToString();
                break;
            }
            decimal BHKGNET = System.Convert.ToDecimal(dwBHSATZ["BHKGNET"]);
           
            #endregion
            #region====Rules======== 
            QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0;
            double BATHNUM = 0;
            if (dwRule != null)
            {
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;//浆纱伸长
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                BATHNUM = Convert.ToDouble(dwRule["JNUM"].ToString());//浆缸数
                JWASTGE = Convert.ToDouble(dwRule["JWASTGE"].ToString());//固定50米
                DELDATE = dwRule["DELDATE"].ToString();
                QTYEND =Convert.ToDouble(dwRule["QTYEND"].ToString());
                LDAB = Convert.ToDouble(dwRule["LEADTIME"].ToString());//生产周期
            }
            if (QTYEND < 0)
            {
                QTYEND = Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 1000;//无放大系数
                
            }
            if (DELDATE.Trim().Length < 8)
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DELDATE.ToString();
            }
            decimal ABPSOLLMENGE5 = Convert.ToDecimal(QTYEND) * BHKGNET * Convert.ToDecimal(0.001);
            var query = from DataRow dwBA in tbABAUFM.AsEnumerable()
                        where dwBA["ABAID"].ToString() == dwABA["ABAID"].ToString()
                        select dwBA;
            foreach (DataRow rx in query)
            {
                decimal beamLg = Convert.ToDecimal(rx["ABASSTKLG"].ToString());
                rx["ABASSTKWG"] = Convert.ToDecimal(beamLg) * Convert.ToDecimal(0.001) * BHKGNET * Convert.ToDecimal(0.001);
                rx["ABARESKG"] = ABPSOLLMENGE5;
            }
         
            JNUM = Convert.ToDouble(dwABA["ABAREUNIT"].ToString());
            double WarpLeng = 0;
            double CWarpleng = 0;
            WarpLeng = Math.Ceiling((QTYEND + F01) / (1 + R01));//经轴长度
            #endregion
            STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB));
            if (STARTD.Trim() == "") STARTD = "0";
            #region=======dwABPN==========
            dwABPN = tmpABPOS.NewRow();
            dwABPN["ABPAENDZL"] = 4;
            dwABPN["ABPMANDANT"] = app.Mandant;
            dwABPN["ABPAUFART"] = "VW";
            dwABPN["ABPAUFKRS"] = "T4";
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPFA"] = 0;
            dwABPN["ABPGA"] = 0;
            dwABPN["ABPQA"] = 0;
            dwABPN["ABPREGEL"] = "00000000";
            dwABPN["ABPLAEAENDA"] = 0;
            dwABPN["ABPZLO"] = PRLO;
            dwABPN["ABPZLB"] = PRLB;
            dwABPN["ABPBLO"] = PRLO;
            dwABPN["ABPGESSTA"] = "TE";
            dwABPN["ABPWBSTA"] = "60";
            dwABPN["ABPWAART"] = "   ";
            dwABPN["ABPMGV"] = 0;
            dwABPN["ABPMGB"] = 0;
            dwABPN["ABPAESTAT"] = " ";
            dwABPN["ABPTMCOUNT"] = 1;
            dwABPN["ABPSERIE"] = "SZ";
            dwABPN["ABPWAKZ"] = "K";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];

            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPSTDLAE"] = 0;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSME"] = "1";
            dwABPN["ABPSOLLMENGE1"] = QTYEND*100;
            dwABPN["ABPSOLLMENGE2"] = WarpLeng* 100;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = ABPSOLLMENGE5;
            dwABPN["ABPSOLLMENGE6"] = ABPSOLLMENGE5;
            dwABPN["ABPSOLLMENGE7"] = JNUM * 100;
            dwABPN["ABPSOLLMENGE8"] = JNUM * 100;
            dwABPN["ABPSZUSATZKZ1"] = " ";
            dwABPN["ABPANZSTK"] = 0;
            dwABPN["ABPZUGETGES"] = 0;
            dwABPN["ABPSTKLG"] = 0;
            dwABPN["ABPKETTAUFT"] = " ";
            dwABPN["ABPDMART"] = "    ";
            if (dwBSGROE != null)
            {
                dwABPN["ABPDMART"] = dwBSGROE["BSGKETTART"];
            }
            tmpABPOS.Rows.Add(dwABPN);
            #endregion
            #region=======ICC_ABAX ================
            var qy = from DataRow row in tbABAUFM.AsEnumerable()
                     where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                     select row;
            foreach (DataRow row in qy)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABAQTYRESERVE"] = QTYEND*100;
            }
            //产生下层需求          
            #region============GAAUF732==========================
            double GAKG1 = 0;
            double GABASMG2 = 0;
            double RT = 1;
            
            double BAGFDZ = 0;           
            filter = "BAGART='" + ART + "' AND BAGVAR='" + VAR + "' AND BAGFARBE='" + FARBE + "'";
            string sort = "BAGPOS ASC";
            rows = db.Tables["BASTGA"].Select(filter,sort);
            double maxcounts = 0;
            double yarncount = 0;
            string YART, YCOLOR;
            bool IsConeDye = false;
            bool IsMixed = false;
            double addweight = 0;
            double BWarpLength = WarpLeng;
            int cdye = 0;
            foreach (DataRow dwBASTGA in rows)
            {
                if (dwBASTGA["BAGAUFMSL"].ToString().Trim() == "")
                {
                    cdye++;
                }
            }
            if (rows.Length > cdye)
            {
                IsMixed = true;
                CWarpleng = BWarpLength + BATHNUM * JWASTGE;
            }
           
            foreach (DataRow dwBASTGA in rows)
            {
                IsConeDye = false;
                RT = Convert.ToDouble(dwBASTGA["BAGKGBRUT"])*0.001*0.001;
                BAGFDZ = Convert.ToDouble(dwBASTGA["BAGFDZ"]);
                YART = dwBASTGA["BAGGART"].ToString();
                YCOLOR = dwBASTGA["BAGGFARBE"].ToString();
                yarncount = RT / BAGFDZ;
                if (dwBASTGA["BAGAUFMSL"].ToString().Trim() == "")
                {
                    IsConeDye = true;
                }
                addweight = 0;
                WarpLeng = BWarpLength;
                if (IsConeDye)
                {
                    if (IsMixed)
                    {
                        WarpLeng = CWarpleng;
                    }
                    string BAGFILLER = dwBASTGA["BAGFILLER"].ToString().Trim();
                    if (BAGFILLER == "")
                    {
                        throw new Exception("请核对织轴工艺!");
                        BAGFILLER = "0";
                    }
                    maxcounts = Convert.ToDouble(BAGFILLER);
                    double ADDLENGTH = Convert.ToDouble(dwBASTGA["ADDLENGTH"].ToString());//保险系数长度
                    if (ADDLENGTH == 0)
                    {
                        if (app.User.Trim() == "JLTV")
                        {
                            ADDLENGTH = 700;
                        }
                        else
                        {
                            throw new Exception("请核对保险系数长度!");
                        }                         
                    }
                    addweight = Math.Ceiling(maxcounts * ADDLENGTH * yarncount);
                    if (RT == 0)
                    {
                        throw new Exception("请核对织轴工艺!");
                    }
                   
                }
                GAKG1 = Math.Ceiling((WarpLeng) * RT) + addweight; 
                dwMU = tbABAUFM.NewRow();
                dwMU.ItemArray = NewABA().ItemArray;
                dwMU["ABAID"] = ABAID; ABAID++;
                #region=====MapRules=======
                dwMU["QTYEND"] = dwRule["QTYEND"];
                dwMU["QTYMAT"] = dwRule["QTYMAT"];
                dwMU["DELDATE"] = dwRule["DELDATE"];
                dwMU["STARTDATE"] = dwRule["STARTDATE"];
                dwMU["LEADTIME"] = dwRule["LEADTIME"];
                dwMU["UNITNUM"] =1;
                dwMU["TTENDS"] = BAGFDZ;
                dwMU["UENDS"] = dwRule["UENDS"];
                dwMU["UNITQTY"] = dwRule["UNITQTY"];
                dwMU["R01"] = dwRule["R01"];
                dwMU["R01DES"] = dwRule["R01DES"];
                dwMU["R02"] = dwRule["R02"];
                dwMU["R02DES"] = dwRule["R02DES"];
                dwMU["F01"] = dwRule["F01"];
                dwMU["F01DES"] = dwRule["F01DES"];
                dwMU["F02"] = BWarpLength * 100;
                dwMU["F02DES"] = addweight;
                dwMU["JNUM"] = dwRule["JNUM"];
                dwMU["JWASTGE"] = dwRule["JWASTGE"];
                dwMU["JWASTAGEDES"] = CWarpleng * 100;
                dwMU["UMS"] = dwRule["UMS"];
                #endregion
                dwMU["ABAMANDANT"] = app.Mandant;
                dwMU["ABAAUFTYP"] = "N";
                dwMU["ABAVKAUFTYP"] = "V";
                dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
                dwMU["ABAVKUPOS"] = dwBASTGA["BAGPOS"];
                dwMU["ABPWAKZ"] = "W";
                dwMU["ABPART"] = ART;
                dwMU["ABPVAR"] = VAR;
                dwMU["ABPFARBE"] = FARBE;
                dwMU["ABACOLORREF"] = dwBASTGA["BAGBEM"]; ;
                dwMU["ABAWAKZ"] = "G";
                dwMU["ABAART"] = dwBASTGA["BAGGART"];
                dwMU["ABAVAR"] = dwBASTGA["BAGGVAR"];
                dwMU["ABAFARBE"] = dwBASTGA["BAGGFARBE"];
                dwMU["ABASSTKLG"] = 0;
                dwMU["ABARESMG"] = GAKG1 * 1000;
                dwMU["ABASANZSTK"] = Math.Ceiling(GAKG1);
                dwMU["ABASSTKGEW"] = "1000";
                dwMU["EXART"] = dwABA["EXART"];
                dwMU["ABAUMART"] = dwBASTGA["BAGAUFMSL"];                
                dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
                dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
                dwMU["ABABASMG"] = dwBASTGA["BAGKGNET"];
                dwMU["ABADISPOKZ"] = dwBASTGA["BAGDISPOKZ"];
                dwMU["ABAABF"] = dwBASTGA["BAGABF"];
                dwMU["ABAAUFMSL"] = "  ";
                dwMU["ABAPACKTYP"] = "    ";
                dwMU["ABAHUELSE"] = "      ";
                dwMU["ABALO"] = PRLO;
                dwMU["ABALB"] = PRLB;
                dwMU["ABALP"] = PRLP;
                dwMU["ABAUSER"] = app.User;
                dwMU["ABADATE"] = date;
                tbABAUFM.Rows.Add(dwMU);
            }
            //修正纬纱数量
            //经纬一样包含保险系数整体加3KG
            //经纬不同，经向+保险系数，纬向+3KG
            /*
            var qWeft = from DataRow dwWeft in tbABAUFM.AsEnumerable()
                        where dwWeft["ABAART"].ToString() == YART
                        && dwWeft["ABAFARBE"].ToString() == YCOLOR
                          && dwWeft["ABPWAKZ"].ToString() == "R"
                        select dwWeft;
            foreach (DataRow dwWeft in qWeft)
            {
                double weft = Convert.ToDouble(dwWeft["ABARESMG"].ToString()) / 1000;
                //3-
                //if (addweight > 1)
                {
                    dwWeft["ABARESMG"] = Math.Ceiling((weft + (3 - addweight)) * 1000);
                    dwWeft["ABASANZSTK"] = Math.Ceiling(weft + (3 - addweight));
                    dwWeft["ABASSTKGEW"] = "1000";
                }
            }*/
            var qWeft = from DataRow dwWeft in tbABAUFM.AsEnumerable()
                          where dwWeft["ABPWAKZ"].ToString() == "R"
                          && dwWeft["ABAWAKZ"].ToString() == "G"
                        select dwWeft;
            foreach (DataRow dwWeft in qWeft)
            {
                  YART=dwWeft["ABAART"].ToString();
                  YCOLOR = dwWeft["ABAFARBE"].ToString();
                  double weft = Convert.ToDouble(dwWeft["ABARESMG"].ToString()) / 1000;
                   filter = "ABAART='" + YART + "' AND ABAFARBE='" + YCOLOR + "' AND ABPWAKZ='W'";
                   rows = tbABAUFM.Select(filter);
                  foreach (DataRow dwWarp in rows)
                  {
                      addweight = Convert.ToDouble(dwWarp["F02DES"].ToString());
                      dwWeft["ABARESMG"] = Math.Ceiling((weft + (3 - addweight)) * 1000);
                      dwWeft["ABASANZSTK"] = Math.Ceiling(weft + (3 - addweight));
                      dwWeft["ABASSTKGEW"] = "1000";
                  }
                  if (rows.Length == 0)
                  {
                      dwWeft["ABARESMG"] = Math.Ceiling((weft +3)* 1000);
                      dwWeft["ABASANZSTK"] = Math.Ceiling(weft + 3);
                      dwWeft["ABASSTKGEW"] = "1000";
                  }
            }
            #endregion
            #endregion 
            SReturn.Merge(tmpABPOS);
            return SReturn;
        }
    }
}